const helper = new Helper(this);
const formEle = this.getElement();
const $form = helper.get('judge-content');
const timeline = helper.get('timelineVertical')?.widgetObj;
let data = $form?.widgetObj._option.dataSource
const selfObj = this.widgetObj;
const options = {}

function loadDict() {
    // 动态
    __drill__services.requestDict('event_trends_type').then((result) => {
        if (result) {
            selfObj.asyncSelectOptions('trends', result);
            __drill__record_form.options['trends'] = result;
        }
    })
    // 所有时间
    __drill__services.requestDict('event_time_scope').then((result) => {
        if (result) {
            selfObj.asyncSelectOptions('timeScope', result);
            __drill__record_form.options['timeScope'] = result;
        }
    })
    // 阶段
    __drill__services.requestDict('event_stage_type').then((result) => {
        if (result) {
            let eventStageType = result.reduce((def, item) => {
                if (['处置中', '未处置', '已处置', '已接警', '未接警'].includes(item.name)) {
                    switch (item.name) {
                        case '处置中': item.name = item.dictName = '演练中'; break;
                        case '未处置': item.name = item.dictName = '未演练'; break;
                        case '已处置': item.name = item.dictName = '已演练'; break;
                        case '未接警': item.name = item.dictName = '未编制'; break;
                        case '已接警': item.name = item.dictName = '已编制'; break;
                    }
                    def.push(item)
                } else {
                    if (item.name.indexOf('接警') === -1 && item.name.indexOf('研判') === -1) {
                        def.push(item)
                    }
                }
                return def
            }, []);

            eventStageType = eventStageType.sort((prev, next) => prev.dataValue - next.dataValue);
            selfObj.asyncSelectOptions('stage', eventStageType)
            __drill__record_form.options['stage'] = result;
        }
    })
}

// 加载数据字典
loadDict();

$(formEle).on('click', '.from-button', () => {
    const $form = helper.get('judge-content')?.widgetObj;
    let data = $form._option.dataSource;
    // 重置表单
    selfObj?.setFormData('form_record', {});
    selfObj?.render();
    const { personName, trends, timeScope, stage } = __drill__record_form.options;
    if (personName) selfObj?.asyncSelectOptions('personName', personName);
    if (trends) selfObj?.asyncSelectOptions('trends', trends);
    if (timeScope) selfObj?.asyncSelectOptions('timeScope', timeScope);
    if (stage) selfObj?.asyncSelectOptions('stage', stage);

    if (data?.id) {
        __drill__services.getProcessRecordByEventId(data.id).then((result) => {
            console.log('事件记录： =》 ', result)
            if (Array.isArray(result)) {
                timeline.dataSource = result;
                timeline.render();
            }
        })
    }
})


